package Constructions.EnemyConstructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import AI.Crew.Crew;
import Constructions.Construction;
import Constructions.ConstructionType;
import Main.SpaceGame;
import MiscelaneousEntities.Explosion;
import Projectiles.Projectile;
import Projectiles.Torpedo;
import World.TileType;

public class EnemyConstruction extends Construction
{
	BufferedImage sprite = null;
	int x,y;
	ConstructionType type;
	SpaceGame main;
	int fireCnt = 0;
	int cooldown;
	int drawCnt = 0;
	boolean drawingLine = false;
	int drawX = 0, drawY = 0;
	
	public EnemyConstruction(int x, int y, SpaceGame main,ConstructionType type) 
	{
		super(x, y, main);
		this.x = x;
		this.y = y;
		this.type = type;
		this.main = main;
		
		setSprite(type.getSpriteURL());
	}
	
	public void update()
	{
		if(type == ConstructionType.Door)
		{
			boolean open = true;
			for(int dx = -1;dx <2;dx++)
			{
				for (int dy = -1;dy<2;dy++)
				{
					if(main.world.tiles[x+dx][y+dy].getType() != TileType.Hull && main.world.tiles[x+dx][y+dy].getOxygen() < 50)
						open = false;
				}
			}
			if(open)
			{
				main.world.tiles[x][y].setSolid(false);
				setSprite("Resources/DoorOpen.png");
			}
			else
			{
				main.world.tiles[x][y].setSolid(true);
				setSprite("Resources/DoorClosed.png");
			}
		}
		
		else if(type == ConstructionType.LifeSupport)
		{
			if(main.world.tiles[x][y].getOxygen() < 98)
			{
				main.world.tiles[x][y].setOxygen(main.world.tiles[x][y].getOxygen()+.8);
			}
		}
		
		else if(type == ConstructionType.TorpedoBay)
		{
			if(fireCnt <= 0)
			{
				if(main.powerManager.nodeList.size() > 0)
				{
					int targetX = 0, targetY = 0;
					if(Math.random() < .1 && main.powerManager.nodeList.get(0) != null)
					{
						targetX = main.powerManager.nodeList.get(0).getX(); targetY = main.powerManager.nodeList.get(0).getY();
					}
					else
					{
						int index = 0;
						do{
						index = (int)(Math.random()*(main.aiManager.entityList.size()-1)+.5);
						}
						while(!(main.aiManager.entityList.get(index) instanceof Crew));
						targetX = main.aiManager.entityList.get(index).getX(); targetY = main.aiManager.entityList.get(index).getY();
					}
					double xVel = Math.cos(Math.atan2(targetY-y, targetX-x))*5;
					double yVel = Math.sin(Math.atan2(targetY-y, targetX-x))*5;
					main.projectileManager.projectileList.add(new Torpedo(x*10, y*10, xVel, yVel, main, false, -7));
					fireCnt = 100;
				}
			}
			else
				fireCnt--;
		}
		else if(type == ConstructionType.PPS)
		{
			if(cooldown <= 0)
			{
				Projectile target = null;
				for(int i = 0;i<main.projectileManager.projectileList.size();i++)
				{
					Projectile current = main.projectileManager.projectileList.get(i);
					if(target == null)
					{
						if(Math.pow(current.x - x*10-.5,2) + Math.pow(current.y - y*10-.5, 2) < 200*200 && current.friendly)
							target = current;
					}
					else if(Math.pow(current.x - x*10-.5,2) + Math.pow(current.y - y*10-.5, 2) < Math.pow(target.x - x*10-.5,2) + Math.pow(target.y - y*10-.5, 2) && current.friendly)
						target = current;
				}
				if(target != null && target.friendly)
				{
					drawCnt = 10;
					cooldown = 100;
					drawX = (int)(target.x+.5)+5;
					drawY = (int)(target.y +.5)+5;
					drawingLine = true;
					main.miscManager.miscList.add(new Explosion((int)(target.x/10+.5), (int)(target.y/10+.5), target.blastRadius, main));
					main.projectileManager.projectileList.remove(target);
					target = null;
				}
			}
			else
				cooldown--;
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) 
	{
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) 
	{
		
	}

	@Override
	public void draw(Graphics g) 
	{
		if(type == ConstructionType.Door || type == ConstructionType.TorpedoBay || type == ConstructionType.PPS)
		{
			if (main.world.tiles[x-1][y].isSolid() && main.world.tiles[x+1][y].isSolid())
			{
				g.drawImage(sprite, x*10, y*10, null);
			}
			else
			{
				double rotationRequired = Math.toRadians(90);
				double locationX = sprite.getWidth() / 2;
				double locationY = sprite.getHeight() / 2;
				AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
				AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

				g.drawImage(op.filter(sprite, null), x*10, y*10, null);				
			}
		}
		else
		g.drawImage(sprite, x*10, y*10, null);
		
		if(drawingLine == true)
		{
			g.setColor(Color.GREEN);
			g.drawLine(x*10, y*10, drawX, drawY);
			g.drawLine(x*10-1, y*10, drawX-1, drawY);
			g.drawLine(x*10+1, y*10, drawX+1, drawY);
			g.drawLine(x*10, y*10-1, drawX, drawY-1);
			g.drawLine(x*10, y*10+1, drawX, drawY+1);
			drawCnt--;
			if(drawCnt <= 0)
				drawingLine = false;
		}
	}

	@Override
	public void setText() 
	{
		
	}

	@Override
	public String getName() 
	{
		return "Enemy Construction";
	}
	
	public void setSprite(String url)
	{
		try {
			sprite = ImageIO.read(URLClassLoader.getSystemResource(url));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
